#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "settingsmanager.h"
#include "contactsmodel.h"
#include "contactsdelegate.h"
#include "chatwidget.h"

#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->menuBar->addAction(ui->actionLogout);

    settings = new SettingsManager;

    // fake contacts list
    QStringList contactsList;
    contactsList << "123" << "asldkjklsajd"
            << "contact1" << "contact2" << "contact3" << "contact4" << "contact5"
            << "contact6" << "contact7" << "contact8" << "contact9" << "cpmtact10"
            << "contact11" << "contact12" << "contact13" << "contact14"
            << "contact15" << "contact16" << "contact17" << "contact18" << "contact19"
            << "contact1" << "contact2" << "contact3" << "contact4" << "contact5"
            << "contact6" << "contact7" << "contact8" << "contact9" << "cpmtact10"
            << "contact11" << "contact12" << "contact13" << "contact14"
            << "contact15" << "contact16" << "contact17" << "contact18" << "contact19"
            << "contact1" << "contact2" << "contact3" << "contact4" << "contact5"
            << "contact6" << "contact7" << "contact8" << "contact9" << "cpmtact10"
            << "contact11" << "contact12" << "contact13" << "contact14"
            << "contact1" << "contact2" << "contact3" << "contact4" << "contact5"
            << "contact6" << "contact7" << "contact8" << "contact9" << "cpmtact10"
            << "contact11" << "contact12" << "contact13" << "contact14"
            << "contact15" << "contact16" << "contact17" << "contact18" << "contact19"
            << "contact1" << "contact2" << "contact3" << "contact4" << "contact5"
            << "contact6" << "contact7" << "contact8" << "contact9" << "cpmtact10"
            << "contact11" << "contact12" << "contact13" << "contact14"
            << "contact15" << "contact16" << "contact17" << "contact18" << "contact19"
            << "contact1" << "contact2" << "contact3" << "contact4" << "contact5"
            << "contact6" << "contact7" << "contact8" << "contact9" << "cpmtact10"
            << "contact11" << "contact12" << "contact13" << "contact14"
            << "contact15" << "contact16" << "contact17" << "contact18" << "contact19";

    ContactsModel *model = new ContactsModel(contactsList);
    ContactsDelegate *delegate = new ContactsDelegate();

    ui->contactsList->setItemDelegate(delegate);
    ui->contactsList->setModel(model);

    // TODO create model and delegate for chats list. Now default behavior used

    emit started();
}

MainWindow::~MainWindow()
{
    delete ui;
    delete settings;
}

void MainWindow::showMainPage()
{
    ui->actionLogout->setVisible(true);
    ui->screensStack->setCurrentIndex(0);
}

void MainWindow::showLoginPage()
{
    ui->actionLogout->setVisible(false);
    ui->screensStack->setCurrentIndex(1);
}

void MainWindow::saveSettings()
{
    QMap<SettingsKey, QVariant> settingsMap;
    settingsMap.insert(UserID, ui->txtID->text());
    settingsMap.insert(Password, ui->txtPassword->text());
    settingsMap.insert(Remember, ui->chRemember->isChecked());

    settings->saveSettings(settingsMap);
}

void MainWindow::loadSettings()
{
    QMap<SettingsKey, QVariant> settingsMap = settings->readSettings();

    ui->txtID->setText(settingsMap.value(UserID).toString());
    ui->txtPassword->setText(settingsMap.value(Password).toString());
    ui->chRemember->setChecked(settingsMap.value(Remember).toBool());
}

void MainWindow::showContactList(QModelIndex modelIndex)
{
    ui->tabs->setCurrentIndex(0);
}

void MainWindow::showChatWindow(QModelIndex modelIndex)
{
    qDebug() << "show chat window for " << modelIndex.data();

    // insert chat in chats window
    //ui->chatsList->model()->insertRow(1);

    //QWidget *newTab = new ChatWidget(this);
    //int newPosition = ui->mainPage->insertTab(ui->mainPage->count(), newTab, "Chat with" + modelIndex.data().toString());
    ui->tabs->setCurrentIndex(1);
    // TODO
}


void MainWindow::login()
{

}

void MainWindow::logout()
{

}

void MainWindow::on_btnSaveSettings_pressed()
{
    this->showMainPage();
    //ui->screensStack->setCurrentIndex(0);
    login();
}

void MainWindow::on_MainWindow_started()
{
    this->showLoginPage();
}
